package br.com.sovi.gwt.client.component;

import br.com.sovi.gwt.client.ContatoCentralPanel;
import br.com.sovi.gwt.client.ImagesImpl;
import br.com.sovi.gwt.client.MenuLateralPanel;
import br.com.sovi.gwt.client.cercaeletronica.CercaCentralPanel;
import br.com.sovi.gwt.client.cercaeletronica.CercaMainMenuPanel;
import br.com.sovi.gwt.client.component.centerpanel.CenterPanel;
import br.com.sovi.gwt.client.component.mainmenu.MainMenuOptionsEnum;
import br.com.sovi.gwt.client.historico.HistoricoCentralPanel;
import br.com.sovi.gwt.client.historico.HistoricoMainMenuPanel;
import br.com.sovi.gwt.client.inicio.InicioCentralPanel;
import br.com.sovi.gwt.client.pesquisa.PesquisaCentralPanel;
import br.com.sovi.gwt.client.pesquisa.PesquisaMenuPanel;
import br.com.sovi.gwt.client.rastreamento.RastreamentoPortal;
import br.com.sovi.gwt.client.rastreamento.RastreamentoTreePanel;

import com.google.gwt.user.client.History;
import com.gwtext.client.widgets.form.Label;

public class TokenChangeController {

	private final MenuLateralPanel menuLateralPanel;

	private final CenterPanel centerPanel;

	private RastreamentoPortal rastreamentoPortal;

	private RastreamentoTreePanel rastreamentoCarroTreePanel;

	public TokenChangeController(MenuLateralPanel menuLateralPanel, CenterPanel centerPanel) {
		this.menuLateralPanel = menuLateralPanel;
		this.centerPanel = centerPanel;
	}

	public void onTokenChange() {
		String token = History.getToken();

		MainMenuOptionsEnum mmoe = MainMenuOptionsEnum.getByToken(token);

		if (mmoe == null) {
			mmoe = MainMenuOptionsEnum.INICIO;
		}

		switch (mmoe) {
		case INICIO:
			showInicioScreen();
			break;
		case RASTREAMENTO:
			showRastramentoScreen();
			break;
		case HISTORICO:
			showHistoricoScreen();
			break;
		case CERCA:
			showCercaScreen();
			break;
		case PESQUISA:
			showPesquisaScreen();
			break;
		case CONTATO:
			showContatoScreen();
			break;

		default:
			break;
		}
	}

	private void showInicioScreen() {
		centerPanel.setTitle(null);
		centerPanel.setContent(new InicioCentralPanel());

		Label label = new Label();

		StringBuilder sb = new StringBuilder();
		sb.append("<table><tr>");
		sb
				.append("<td><div style='font-size: 12px; font-weight: bold; font-family: Arial'>Escolha uma das opções do menu acima.</div></td>");
		sb.append("<td>").append(ImagesImpl.get().up().getHTML()).append("</td>");
		sb.append("</tr></table>");

		label.setHtml(sb.toString());

		menuLateralPanel.setContent(label);
		menuLateralPanel.collapse(true);
	}

	private void showRastramentoScreen() {
		centerPanel.setTitle("Rastreamento");
		rastreamentoPortal = new RastreamentoPortal();
		centerPanel.setContent(rastreamentoPortal);

		rastreamentoCarroTreePanel = new RastreamentoTreePanel();
		menuLateralPanel.setContent(rastreamentoCarroTreePanel);
		rastreamentoCarroTreePanel.setRastreavelCheckChangeListener(rastreamentoPortal);
		menuLateralPanel.expand(true);
	}

	private void showHistoricoScreen() {
		centerPanel.setTitle("Histórico");
		final HistoricoCentralPanel panel = new HistoricoCentralPanel();
		centerPanel.setContent(panel);

		menuLateralPanel.setContent(new HistoricoMainMenuPanel(panel, panel));
		menuLateralPanel.expand(true);
	}

	private void showCercaScreen() {
		centerPanel.setTitle("Cerca Eletrônica");

		CercaCentralPanel cercaCentralPanel = new CercaCentralPanel();
		centerPanel.setContent(cercaCentralPanel);

		menuLateralPanel.setContent(new CercaMainMenuPanel(cercaCentralPanel));
		menuLateralPanel.expand(true);
	}

	private void showPesquisaScreen() {
		centerPanel.setTitle("Pesquisas");
		PesquisaCentralPanel panel = new PesquisaCentralPanel();
		centerPanel.setContent(panel);

		menuLateralPanel.setContent(new PesquisaMenuPanel(panel));
		menuLateralPanel.expand(true);
	}

	private void showContatoScreen() {
		centerPanel.setTitle(null);
		centerPanel.setContent(new ContatoCentralPanel());

		Label label = new Label();

		StringBuilder sb = new StringBuilder();
		sb.append("<table><tr>");
		sb
				.append("<td><div style='font-size: 12px; font-weight: bold; font-family: Arial'>Escolha uma das opções do menu acima.</div></td>");
		sb.append("<td>").append(ImagesImpl.get().up().getHTML()).append("</td>");
		sb.append("</tr></table>");

		label.setHtml(sb.toString());

		menuLateralPanel.setContent(label);
		menuLateralPanel.collapse(true);

	}
}
